#!/bin/bash
touch login.log
touch loginer.log
m=1
while [ $m != 0 ]
    do
    read -r -p 'localhost login: ' usrname
    read -r -s -p 'Password: ' pwd
    if [ "${usrname}" == "tom" ] && [ "${pwd}" == "tom" ]
    then
        lastfail="$(tail -n 1 loginer.log)"
        lastlog="$(tail -n 1 login.log)"
        if [ "$lastlog" != " " ] 
        then
            echo "Last login: $lastlog"
        fi
        if [ "$lastfail" != "" ] 
        then
            echo "Last failed login: $lastfail"
        fi
        echo "$(date) On $(tty)" >> login.log
        echo "welcome"
        m=0
    else
        m=$((m+1))
        echo "login failed."
        echo "$(date) On $(tty)" >> loginer.log
        if [ $m -gt 3 ]
        then
            echo "Failed 3 times.Please check your ID or password "
            break
        else
            read -r
        fi
    fi
done